home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Graphics Samples / Bouncing Bitmap ƒ / Bouncing Bitmap.c next >
Encoding:
C/C++ Source or Header  |  1993-09-14  |  5.7 KB  |  188 lines  |  [TEXT/KAHL]

  1. /*
  2.     Bouncing bitmap
  3.     
  4.     This application will get a pixmap from its resource file.  It will scale it to a smaller size.
  5.     The pixmap (i.e. gBitmapShape) will then bounce around the window. We check to make sure the 
  6.     bitmapShape will be drawn within the bounds of the window by checking the location of the 
  7.     bitmapShape against the windowBoundsShape before each GXDrawShape call. The bitmapShape will be 
  8.     rotated 12 degrees before being drawn by GXDrawShape.  
  9.  
  10.     NOTES:
  11.     • This file requires the following files to run correctly:
  12.         "graphics shell.c", "graphics debug library.c", "qd library.c", "transform library.c".
  13.     
  14.     • For the best printing results, print this file in "landscape".
  15.  
  16.     ©1990 - 1993 Apple Computer, Inc.
  17.     All rights reserved.
  18. */
  19.  
  20.  
  21. #include <Events.h>
  22. #include <Windows.h>
  23.  
  24. #include "font library.h"
  25. #include "graphics toolbox.h"
  26. #include "graphics libraries.h"
  27. #include "graphics debugging.h"
  28. #include "qd library.h"
  29. #include "graphics shell.h"
  30.  
  31. //
  32. //  Set up the title and size of the window 
  33. //
  34. Rect         gWindowQDRect = {40, 20, 340, 440};
  35. Str255         gWindowTitle = "\pBouncing bitmap";
  36.  
  37. //
  38. //    If gDebugging = TRUE, graphics library errors and notices will be posted.  This functionality  will only work with 
  39. //    the "debugging" version of the QuickDraw GX init.  If this version of the init is not installed, nothing bad will happen, 
  40. //    but these  functions will not work. 
  41. //
  42. Boolean        gDebugging = true;
  43.  
  44.  
  45. //
  46. //     Set  "gGiveMeValidation" to TRUE, if you will receive run-time validation.  
  47. //
  48. Boolean        gGiveMeValidation = true;
  49.  
  50.  
  51. //
  52. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  53. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  54. //    With  gGraphicsHeapSize set to 60k, I had 5 free blocks left in the graphics gxHeap. Sounds good to me.
  55. //
  56. long        gGraphicsHeapSize = 80;
  57.  
  58. gxShape     gBitmapShape;
  59. gxRectangle    gBitmapShapeBounds;
  60. fixed         gDx, gDy, gDelta;
  61. gxRectangle gFixedWindowBounds;           //  bounding box of the window in local coordinates 
  62.  
  63.  
  64.  
  65. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  66.  
  67. void DoInitialization(theWindow)
  68. WindowPtr theWindow;
  69. {
  70.     gxShape clipShape;
  71.  
  72.     //
  73.     //    Get the bounds of the window in GX coordinates.  We will use this information in the 
  74.     //    DoIdle function to make sure "Clarus" does not bounce outside of the window.
  75.     //
  76.     GXGetShapeBounds(gWindowBoundsShape, 0L, &gFixedWindowBounds);
  77.  
  78.     //
  79.     //    Get the pixmap from the a resource file. If we fail in our attempt to create gBitmapShape, 
  80.     //    GXValidateShape will let us know via the debugger. 
  81.     //
  82.     gBitmapShape = GetPixMapShape(128);
  83.     GXValidateShape (gBitmapShape);
  84.  
  85.       GXScaleShape(gBitmapShape, fixed1/2, fixed1/2, 0, 0);
  86.  
  87.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  88.  
  89.     //
  90.     //  Setup the clip gxShape of the gBitmapShape
  91.     //
  92.     clipShape = GXNewRectangle(&gBitmapShapeBounds);
  93.     GXSetShapeClip(gBitmapShape, clipShape);
  94.     GXDisposeShape(clipShape);
  95.  
  96.     gDx = ff(5) + fixed1/2;
  97.     gDy = ff(6);
  98. }
  99.  
  100.  
  101.  
  102. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  103.  
  104. void DoIdle(theWindow)
  105. WindowPtr theWindow;
  106. {
  107.     gxMapping         map;
  108.     gxShape         boundsShape;
  109.  
  110.     GXRotateShape(gBitmapShape, ff(12), (gBitmapShapeBounds.left + gBitmapShapeBounds.right >> 1), (gBitmapShapeBounds.top + gBitmapShapeBounds.bottom >> 1));
  111.  
  112.     //
  113.     //  Get the bounds of the gBitmapShape and setup a gxRectangle that contains the bounds.
  114.     //
  115.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  116.     boundsShape = GXNewRectangle (&gBitmapShapeBounds);
  117.     
  118.     //
  119.     //    Get the gxMapping of the gBitmapShape and set boundsShape mapping to it. We use boundsShape
  120.     //    to setup the "new" "bounds" of our bouncing gBitmapShape. "bounds" is then used below to make
  121.     //    sure that "Clarus" does not bounce outside of the window.
  122.     //
  123.     GXGetShapeMapping(gBitmapShape, &map);
  124.     GXMapShape(boundsShape, &map);
  125.     GXGetShapeBounds(boundsShape, 0L, &gBitmapShapeBounds);
  126.  
  127.     //
  128.     //    Make sure "Marilyn" doe not bounce outside of the window, by adjusting the "new" location
  129.     //    appropriately.
  130.     //
  131.     if ((gDelta = gFixedWindowBounds.left - gBitmapShapeBounds.left) > 0 || (gDelta = gFixedWindowBounds.right - gBitmapShapeBounds.right) < 0)
  132.      {    
  133.         GXMoveShape(gBitmapShape, gDx + gDelta, ff(0));    
  134.         gDx = -gDx;    
  135.      }
  136.  
  137.  
  138.     if ((gDelta = gFixedWindowBounds.top - gBitmapShapeBounds.top) > 0 || (gDelta = gFixedWindowBounds.bottom - gBitmapShapeBounds.bottom) < 0)
  139.      {    
  140.          GXMoveShape(gBitmapShape, ff(0), gDy + gDelta);    
  141.          gDy = -gDy;
  142.      }
  143.  
  144.     GXMoveShape(gBitmapShape, gDx, gDy);
  145.     GXDrawShape(gBitmapShape);
  146.  
  147.     GXDisposeShape(boundsShape);
  148. }
  149.  
  150.  
  151. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  152.  
  153. void DoDraw(theWindow)
  154. WindowPtr theWindow;
  155. {
  156.     GXDrawShape(gBitmapShape);
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*------ DoDispose ------------------------------------------------------------------------------------*/
  163.  
  164. void DoDispose(theWindow)
  165. WindowPtr theWindow;
  166. {
  167.     //  
  168.     //    You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  169.     //    form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  170.     //    call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  171.     //    SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  172.     //    can turn notices on in this file by setting gDebugging = TRUE (above).
  173.     //  
  174.     GXDisposeShape(gBitmapShape);
  175.     GXDisposeShape(gWindowBoundsShape);
  176.        DisposeWindow(theWindow);
  177. }
  178.  
  179.  
  180.  
  181. /*------ DoClick ---------------------------------------------------------------------------------------*/
  182.  
  183. void DoClick(theWindow)
  184. WindowPtr theWindow;
  185. {
  186. }
  187.  
  188.